<?php
// +----------------------------------------------------------------------
// | Description: 组织架构
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\model\User;
use think\facade\Db;
use app\admin\model\Structure as StructureModel;
use think\response\Json;

class Structures extends ApiCommon
{
    /**
     * 用于判断权限
     * @permission 无限制
     * @allow 登录用户可访问
     * @other 其他根据系统设置
     **/
    public function initialize()
    {
        $action = [
            'permission'=>[],
            'allow'=>['index', 'selectstructure', 'selectparent']
        ];
        event('CheckAuth', $action);
        $request = $this->request;
        $a       = strtolower($request->action());
        if (!in_array($a, $action['permission'])) {
            parent::initialize();
        }
    }

    /**
     * 部门列表
     * @author Mr.Li
     * @param
     * @return
     */
    public function index()
    {
        $structureModel = new StructureModel();
        $param = $this->param;
        $type = isset($param['type']) && $param['type'] ? 'tree' : '';
        $data = $structureModel->getDataList($type,$param);
        return resultArray(['data' => $data]);
    }

    /**
     * 部门列表
     * @param  StructureModel $structureModel
     * @return Json
     * @author Mr.Liu Wuchen
     * @create 2021-07-27 15:07
     */
    public function list(StructureModel $structureModel): Json
    {
        $data = $structureModel->getDataList('tree');
        if (!$data) {
            return resultArray(['error' => $structureModel->getError()]);
        }
        return resultArray(['data' => $data]);
    }

    /**
     * 详情
     * @param  StructureModel $structureModel
     * @return Json
     * @author Mr.Liu Wuchen
     * @create 2021-06-23 14:09
     */
    public function read(StructureModel $structureModel): Json
    {
        $this->param['user_id'] = $this->userInfo['id'];
        $data = $structureModel->getDataInfo($this->param);
        if (!$data) {
            return resultArray(['error' => $structureModel->getError()]);
        }
        return resultArray(['data' => $data]);
    }

    /**
     * 添加部门
     * @param  StructureModel $structureModel
     * @return Json
     * @author Mr.Liu Wuchen
     * @create 2021-07-27 16:21
     */
    public function create(StructureModel $structureModel): Json
    {
        $this->param['user_id'] = $this->userInfo['id'];
        $data = $structureModel->addData($this->param);
        if (!$data) {
            return resultArray(['error' => $structureModel->getError()]);
        }
        return resultArray(['data' => [],'error' => '添加成功']);
    }

    /**
     * 编辑部门
     * @param  StructureModel $structureModel
     * @return Json
     * @author Mr.Liu Wuchen
     * @create 2021-07-27 16:24
     */
    public function update(StructureModel $structureModel): Json
    {
        $this->param['user_id'] = $this->userInfo['id'];
        $data = $structureModel->saveData($this->param);
        if (!$data) {
            return resultArray(['error' => $structureModel->getError()]);
        }
        return resultArray(['error' => $structureModel->getError(), 'data'=>[]]);
    }

    /**
     * 删除部门
     * @param  StructureModel $structureModel
     * @return Json
     * @author Mr.Liu Wuchen
     * @create 2021-06-23 15:07
     */
    public function delete(StructureModel $structureModel): Json
    {
        $this->param['user_id'] = $this->userInfo['id'];
        $data = $structureModel->deleteData($this->param);
        if (!$data) {
            return resultArray(['error' => $structureModel->getError()]);
        }
        return resultArray(['error' => $structureModel->getError(), 'data'=>[]]);
    }

    /**
     * 部门操作记录
     * @param  StructureModel $structureModel
     * @return Json
     * @author Mr.Liu Wuchen
     * @create 2021-07-28 11:30
     */
    public function record(StructureModel $structureModel): Json
    {
        $this->param['user_id'] = $this->userInfo['id'];
        $data = $structureModel->getRecordList($this->param);
        if (!$data) {
            return resultArray(['error' => $structureModel->getError()]);
        }
        return resultArray(['data' => $data]);
    }

    /**
     * 迁移选择部门
     * @param  StructureModel $structureModel
     * @return Json
     * @author Mr.Liu Wuchen
     * @create 2021-07-29 15:40
     */
    public function selectStructure(StructureModel $structureModel): Json
    {
        $this->param['user_id'] = $this->userInfo['id'];
        $data = $structureModel->getSelectStructureData($this->param);
        if (!$data) {
            return resultArray(['error' => $structureModel->getError()]);
        }
        return resultArray(['error' => $structureModel->getError(), 'data' => $data['data']]);
    }

    /**
     * 迁移选择上级
     * @param  StructureModel $structureModel
     * @return Json
     * @author Mr.Liu Wuchen
     * @create 2021-07-29 15:40
     */
    public function selectParent(StructureModel $structureModel): Json
    {
        $this->param['user_id'] = $this->userInfo['id'];
        $data = $structureModel->getSelectParentData($this->param);
        if (!$data) {
            return resultArray(['error' => $structureModel->getError()]);
        }
        return resultArray(['error' => $structureModel->getError(), 'data' => $data['data']]);
    }

    /**
     * 部门迁移
     * @param  StructureModel $structureModel
     * @return Json
     * @author Mr.Liu Wuchen
     * @create 2021-07-29 15:15
     */
    public function move(StructureModel $structureModel): Json
    {
        $this->param['user_id'] = $this->userInfo['id'];
        $data = $structureModel->moveData($this->param);
        if (!$data) {
            return resultArray(['error' => $structureModel->getError()]);
        }
        return resultArray(['error' => $structureModel->getError(), 'data'=>[]]);
    }

    /**
     * 部门绑定省市区
     * @param  StructureModel $structureModel
     * @return Json
     * @author SL
     * @create 2021-07-30 16:21
     */
    public function saveRegion(StructureModel $structureModel): Json
    {
        $this->param['user_id'] = $this->userInfo['id'];
        $data = $structureModel->saveRegionData($this->param);
        if (!$data) {
            return resultArray(['error' => $structureModel->getError()]);
        }
        return resultArray(['data' => [],'error' => '操作成功']);
    }

    /**
     * 部门绑定区域详情
     * @param  StructureModel $structureModel
     * @return Json
     * @author SL
     * @create 2021-07-31 14:09
     */
    public function region(StructureModel $structureModel): Json
    {
        $this->param['user_id'] = $this->userInfo['id'];
        $data = $structureModel->getRegionData($this->param);
        if (!$data) {
            return resultArray(['error' => $structureModel->getError()]);
        }
        return resultArray(['data' => $data]);
    }

}